#include <iostream>


#include <LinuxTwoWire.h>
#include <Adafruit_SSD1306.h>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <unistd.h>

#include "FreeSans9pt7b.h"
#include "FreeSerifBoldItalic24pt7b.h"
#include "FreeSerifItalic12pt7b.h"
#include "FreeSerifItalic18pt7b.h"
#include "FreeSerifItalic24pt7b.h"
#include "FreeSerifItalic9pt7b.h"
#include "Org_01.h"
#include "Picopixel.h"
#include "Tiny3x3a2pt7b.h"
#include "TomThumb.h"

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

#define OLED_RESET     (-1)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32

TwoWire wire("/dev/i2c-2");

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &wire);

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16

void delay(const long ms)
{
    usleep(ms * 1000);
}

int main()
{
    if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
        std::cerr << "SSD1306 allocation failed" << std::endl;
        exit(-1);
    }
    display.set_gfx_font(&FreeSans9pt7b);

    display.display();
    delay(10); // Pause for 2 seconds

    // Clear the buffer
    display.clearDisplay();
    delay(1000);
    display.invertDisplay(true);
    delay(1000);
    display.invertDisplay(false);
    for (int i = 0; i < 10000; ++i) {
        auto now = std::chrono::system_clock::now(); // 获取当前时间点
        std::time_t now_c = std::chrono::system_clock::to_time_t(now); // 转换为time_t
        std::tm local_tm = *std::localtime(&now_c); // 转为本地时间
        std::ostringstream oss;
        oss << std::put_time(&local_tm, "%Y-%m-%d\n%H:%M:%S");
        std::string time_str = oss.str();
        display.clearDisplay();
        display.printf(0, 0,SSD1306_WHITE, "%s", time_str.c_str());
        display.display();
        delay(500);
    }
    for (uint8_t h = 0; h < SCREEN_HEIGHT; h++) {
        for (uint8_t w = 0; w < SCREEN_WIDTH; w++) {
            display.drawPixel(w, h, SSD1306_WHITE);
            if (w % 50 == 0) {
                display.display();
            }
        }
    }
    display.display();

}
